﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;
using System.Web.Routing;
using ShopB2C.Entity;
using ShopB2C.Enums;
using ShopB2C.Helper;
using ShopB2C.Interfaces;

namespace ShopB2C.Controllers.Manager
{
    /// <summary>
    /// 首页配置
    /// </summary>
    public partial class ConfigController : BaseController
    {
        private static IProductPreview _productView;
        private static IClassify _classify;
        private static IImageRepeat _ir;
        private static IProductSearchKeys _psk;
        private static INews _news;
        private static IProductOperate _productOperate;

        private static string _irPath;
        private const string _moduleKey = "Product";

        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            _irPath = this.Server.MapPath("~/OtherPictures/");
        }

        public ConfigController()
        {
            _productView = DataBaseExecuter.CreateClassByName<IProductPreview>("ProductPreView");
            _classify = DataBaseExecuter.CreateClassByName<IClassify>("Classify");
            _ir = DataBaseExecuter.CreateClassByName<IImageRepeat>("ImageRepeat");
            _psk = DataBaseExecuter.CreateClassByName<IProductSearchKeys>("ProductSearchKeys");
            _news = DataBaseExecuter.CreateClassByName<INews>("News");
            _productOperate = DataBaseExecuter.CreateClassByName<IProductOperate>("ProductOperate");

            this.PageSize = 13;
            ViewData["ImageRoot"] = _imageRoot;
        }
        /// <summary>
        /// 首页配置
        /// </summary>
        public ActionResult HomeConfig()
        {
            return View();
        }

        #region 产品模块
        [OutputCache(Duration = 0 , VaryByParam = "*")]
        public ActionResult Product()
        {

            ViewData["PartTypeList"] = Methods.GetEnumsToList(typeof(PartType));

            List<ProductPartEntity> parts = _productView.GetPartList(null);
            return View(parts);
        }
        [HttpPost]
        public ActionResult Product(int? c)
        {
            if (Request.Form["PartId"] != null)
            {
                string[] ivs = Request.Form.GetValues("IsValid");
                string[] ses = Request.Form.GetValues("Sequence");
                string[] ids = Request.Form.GetValues("PartId");

                string id , se , iv;
                id = se = iv = string.Empty;

                for (int i = 0 ; i < ids.Length ; i++)
                {
                    id += string.Format("{0}," , ids[i]);
                    se += string.Format("{0}," , ses[i]);
                    iv += string.Format("{0}," , Convert.ToBoolean(ivs[i]) ? 1 : 0);
                }

                id = id.Trim(',');
                iv = iv.Trim(',');
                se = se.Trim(',');

                int result = _productOperate.ProductPartUpdate(id , iv , se);

            }


            return new EmptyResult();
        }

        /// <summary>
        /// 产品模块
        /// </summary>
        [HttpPost]
        public ActionResult PartPost(
            [DefaultValue("")]string title
            , [DefaultValue(false)] bool isValid
            , [DefaultValue(0)]int partType
            , [DefaultValue(8)]int count
             , [DefaultValue(0)]int classId
            , [DefaultValue(0)]int sequence
             , [DefaultValue(0)]int partId
            , [DefaultValue("Home")]string moduleKey
               )
        {
            string ids = string.Empty;

            if (Request.Form["ProductId"] != null && partType < 0)
            {
                foreach (string item in Request.Form.GetValues("ProductId"))
                {
                    ids += string.Format("{0}," , item);
                }
                ids = ids.Trim(',');
            }
            int result = _productOperate.ProductPartSave(partId , title , isValid , partType , count , classId , sequence , ids , moduleKey);
            return new EmptyResult();
        }
        /// <summary>
        /// 插入或修改框
        /// </summary>
        [HttpGet]
        public ActionResult PartPost([DefaultValue(0)]int partId)
        {
            ViewData["ClassList"] = _classify.GetClassifyByModuleKey(_moduleKey);
            ViewData["PartTypeList"] = Methods.GetEnumsToList(typeof(PartType));
            ProductPartEntity part = _productView.GetProductPart(partId , null);
            part = part ?? new ProductPartEntity() { ProductList = new List<ProductEntity>() , IsVaild = true , Count = 4 };
            part.ModuleKey = "Home";
            return View(part);
        }
        /// <summary>
        /// 删除部件
        /// </summary>
        public ActionResult PartDelete(int partId)
        {
            _productOperate.ProductPartDelete(partId);
            return new EmptyResult();
        }

        #endregion
    }
}
